//
//  AppDelegate.swift
//  Weibo
//
//  Created by 孙成宇 on 15/11/26.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        print(path)
        let account = UserAccount.loadAccount()
        print(account)

        //设置主题色
       setThemeColor()
        
        //手动实例化window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.makeKeyAndVisible()
        
        registerNotification()
      window?.rootViewController = defaultRootViewController()

        return true
    }

    private func defaultRootViewController() -> UIViewController {

        if BaseTableViewController().userLogin {
        
        //是否是新版本
            if isNewVersion() {
            
            return NewFeatureViewController()
            
            }
            return WelcomeViewController()
        
        
        }
        return MainViewController()

    }
    
    
    @objc private func switchRootVC(n:NSNotification) {
    
    
    print(n)
        window?.rootViewController = n.object != nil ?
            MainViewController() : WelcomeViewController()
    }
    
    private func registerNotification() {
    
    
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootVC:", name: WBSwitchRootVCNotification, object: nil)
        
        print("---")
    }
    
    
    
    
    
    
    
    
    private func setThemeColor() {
        //设置导航条文案渲染颜色
        UINavigationBar.appearance().tintColor = themeColor
        UITabBar.appearance().tintColor = themeColor
    }
  
    
    
    
    //判断是否是新版本
    private func isNewVersion() -> Bool {
    
    
    let dict = NSBundle.mainBundle().infoDictionary!
    let currentNum = (dict["CFBundleShortVersionString"]as! NSString).doubleValue
        
      let sandBoxKey  = "sandBoxKey"
        let defaults = NSUserDefaults.standardUserDefaults()
        let lastVersion = defaults.doubleForKey(sandBoxKey)
        
        defaults.synchronize()
        return currentNum > lastVersion
        
    
    
    }
    
    
    
    
    
}

